{template 'web/common/header'}
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span>单位设置</span>
                    <button class="layui-btn layui-btn-sm" data-type="unitAdd" id="unitAdd">增加</button>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="unit-table" lay-filter="unit"></table>
                    <script type="text/html" id="unit-table-operate-bar">
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                  </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../addons/xn_erp/images/layuiadmin/layui/layui.js"></script>
<script>
    layui.use(['table', 'element'], function () {
        var table = layui.table, $ = layui.$, element = layui.element;
        element.render();
        // 渲染表格
        table.render({
            elem: '#unit-table'
            , url: '{$url}&api=unit_list'
            , title: '单位设置'
            , cols: [[
                { field: 'id', width: 100, align: "center", title: 'ID', fixed: 'left', sort: true, }
                , { field: 'name', align: "center", title: '名称', edit: 'text' }
                , { width: 178, align: 'center', title: "操作", fixed: 'right', toolbar: '#unit-table-operate-bar' }
            ]]
            , page: true
            , limit: 20
            ,method:'post'
            , id: 'unitReload'
        });
        // 删除
        table.on('tool(unit)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.post("{$url}&api=unit_del", { uid: data.id }, function (result) { layer.msg(result); });
                    obj.del();
                    layer.close(index);
                });
            }
        });
        // 修改
        table.on('edit(unit)', function (obj) {
            var data = obj.data;
            $.post("{$url}&api=unit_edit", { uid: data.id, name: data.name }, function (result) { layer.msg(result); });
        });
        // 增加
        $('#unitAdd').on('click', function () {
            layer.prompt({ title: '请输入新的单位',offset: '300px'}, function (val, index) {
                $.post("{$url}&api=unit_add", { name: val }, function (result) { layer.msg(result); });
                table.reload('unitReload');
                layer.close(index);
            });
        });

    });
</script>

</body>

</html>